﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using Context;
namespace WCF_ListenMusic.Services.Module_Album.Admin
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ManageAlbumService" in code, svc and config file together.
    public class ManageAlbumService : IManageAlbumService
    {
        Context.Connect_DBDataContext context = new Context.Connect_DBDataContext();

        public List<AlbumSinger> getAllAlbum()
        {
            try {
                var q = from a in context.AlbumSingers
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException (e.Message);}
        }

        public int countSonginAlbum(int albumid)
        {
            try
            {
                var q = (from a in context.Album_Songs
                        where a.AlbumID.Equals(albumid)
                        select a).Count();
                return q;
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }

       
        public int getMax_AlbumID()
        {
            try
            {
                var q = (from a in context.Albums                        
                         select a.AlbumID).Max();
                return q;
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public bool insertNewAlbum(Album a)
        {
            try
            {                
                context.Albums.InsertOnSubmit(a);
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public bool uploadAlbumSong(Album_Song albumSong)
        {
            try
            {
                
                context.Album_Songs.InsertOnSubmit(albumSong);
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }


       
        public int getMaxID_AlbumSong()
        {
            try
            {
                var q = (from a in context.Album_Songs
                         select a.Album_SongID).Max();
                return q;
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public AlbumSinger getAlbum_byID(int albumid)
        {
            try
            {
                var q = from a in context.AlbumSingers
                        where a.AlbumID.Equals(albumid)
                        select a;
                return q.SingleOrDefault();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public bool deleteSonginAlbum(int songid)
        {
            try
            {
                var q = from a in context.Album_Songs
                        where a.SongID.Equals(songid)
                        select a;
                Album_Song s = q.Single();
                context.Album_Songs.DeleteOnSubmit(s);
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public bool updateAlbum(Album album)
        {
            try
            {
                var q = from a in context.Albums
                        where a.AlbumID.Equals(album.AlbumID)
                        select a;
                Album aOld = q.Single();
                aOld.AlbumName = album.AlbumName;
                aOld.CategoryID = album.CategoryID;
                aOld.CountryID = album.CountryID;
                aOld.Description = album.Description;
                aOld.ReleaseYear = album.ReleaseYear;
                aOld.Picture = album.Picture;
                aOld.SingerID = album.SingerID;
                context.SubmitChanges();
                return true;
            }
            catch (Exception e) { return false; throw new FaultException(e.Message); }
        }
    }
}
